package com.wing.sys.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.cms.domain.Layout;
import com.wing.common.CommonSpringJDBC;
import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Auth;
import com.wing.sys.domain.User;

@Repository
public class UserDAO extends CommonSpringJDBC<User,String> {

	public void delete(final String id) {
		String sql = "update sys_user  set valid_flag = -1 where id = ?";
		this.getSimpleJdbcTemplate().update(sql, id);
	}
	
	public List<Map<String, Object>> findAll() {
		String sql = "select * from sys_user where (valid_flag is null or valid_flag != -1) ";
		return super.getSimpleJdbcTemplate().queryForList(sql);
	}

	public List<Map<String, Object>> findUsersByOrgId(String orgId) {
		String sql = "select u.* from sys_user u , sys_org_user ou where ou.userid=u.id and ou.orgid=? and (u.valid_flag is null or u.valid_flag != -1)";
		return super.getSimpleJdbcTemplate().queryForList(sql,orgId);
	} 
	

	public PageBean findPageList(int currentPage, int pageSize, String seachv,String siteId) {
		String where= " (u.valid_flag is null or u.valid_flag != -1) ";
		if(seachv!=null && !"".equals(seachv)){
			where = " (u.valid_flag is null or u.valid_flag != -1) and (u.name like '%"+seachv+"%' or u.login_code like '%"+seachv+"%' or u.email like '%"+seachv+"%') ";
		}
		if(!Constants.WORK_SITE.equals(siteId)){
			where = where+" and u.site_id='"+siteId+"' ";
		}
		String sql1=" SELECT u.* FROM sys_user u where "+where+" ORDER BY u.order_num ASC,u.createtime desc " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(u.id) from sys_user u   where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List<Map<String, Object>> findUser(String login_code, String password) {
		String sql = "select * from sys_user t where login_code = ? and password=? and (valid_flag is null or valid_flag != -1)";
		Object [] objs = {login_code,password};
		return super.getSimpleJdbcTemplate().queryForList(sql,objs);
	}

	/**
	 * 根据userid获取该用户的所有角色
	 * @param id
	 * @return
	 */
	public PageBean findPageRolesByUserId(int currentPage, int pageSize,
			String userId) {
		String where= " ru.roleid=r.id and ru.userid='"+userId+"' and (r.valid_flag is null or r.valid_flag != -1) ";
		String sql1=" SELECT r.* FROM sys_role_user ru,sys_role r where "+where+" ORDER BY r.id ASC  " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(r.id) from sys_role_user ru,sys_role r  where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List findUsersByLoginCode(String lgcode) {
		String sql = "select * from sys_user t where login_code = ? and (valid_flag is null or valid_flag != -1)";
		return super.getSimpleJdbcTemplate().queryForList(sql,lgcode);
	}

	public PageBean findPageOrgsByUserId(int currentPage, int pageSize,
			String userId) {
		String where= " uo.orgid=o.id and uo.userid='"+userId+"' and (o.valid_flag is null or o.valid_flag != -1) ";
		String sql1=" SELECT o.* FROM sys_user_org uo,sys_organization o where "+where+" ORDER BY o.order_num ASC,o.id ASC " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(o.id) from sys_user_org uo,sys_organization o  where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}
}
